package com.gange.controller;


import com.gange.entity.Category;
import com.gange.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 分类 前端控制器
 * </p>
 *
 * @author gange
 * @since 2022-11-02
 */
@RestController
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/categories")
    public Category addCategory(@RequestBody Category category){
        //category是缺属性的。
        category.setCreatedAt(LocalDateTime.now());
        category.setUpdatedAt(LocalDateTime.now());
        categoryService.save(category);
        return category;
    }
    @GetMapping("/categories")
    public List<Category> selectCateTree(){
        return categoryService.selectCateTree();
    }
    @GetMapping("/selectOne/{id}")
    public Category selectById(@PathVariable("id") int id){
        return categoryService.getById(id);
    }
}

